package com.icbc.open.chengdu.demo.account.query;

import com.csii.pp.icbc.util.MD5Util;
import com.csii.pp.icbc.util.Util;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.internal.util.internal.util.fastjson.JSON;
import com.icbc.open.chengdu.demo.BaseData;

import com.icbc.api.request.SettlementAccountStatusQueryRequestV1;
import com.icbc.api.request.SettlementAccountStatusQueryRequestV1.SettlementAccountStatusQueryRequestV1Biz;
import com.icbc.api.response.SettlementAccountStatusQueryResponseV1;
import com.icbc.open.chengdu.demo.utils.GenerateKeyUtils;
import com.icbc.open.chengdu.demo.utils.SecretUtils;

/**
 * 账户状态查询
 *
 * @author xcz
 */
public class SettlementAccountStatusQueryTest {

	public static void main(String[] args) throws Exception {
		String sm4Key = GenerateKeyUtils.generateSM4Key(); //生成SM4密钥
		DefaultIcbcClient client = new DefaultIcbcClient(BaseData.APP_ID, BaseData.MY_RSA_PRIVATE_KEY, BaseData.ICBC_RSA_PUBLIC_KEY);
	    SettlementAccountStatusQueryRequestV1 request = new SettlementAccountStatusQueryRequestV1();
		SettlementAccountStatusQueryRequestV1Biz bizContent = new SettlementAccountStatusQueryRequestV1Biz();
		request.setServiceUrl(BaseData.ACCOUNT_STATUS_QUERY_URL);
		bizContent.setCorpMediumId(BaseData.TEST_CORP_MEDIUM_ID); //合作方介质号
		bizContent.setCorpNo(BaseData.APP_ID); //合作方机构编号
		bizContent.setCorpSerno(Util.getNonceStr(16)); //交易单号
		bizContent.setOutServiceCode("querystatus"); //外部服务代码
		bizContent.setSecretKey(SecretUtils.encryptSM2(sm4Key)); //SM2公钥加密的sm4对称密钥
		bizContent.setMediumId(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_ACCOUNT_NUMBER_2)); //工行联名卡号(支持加密，Base64编码)
		bizContent.setMediumIdHash(MD5Util.string2MD5(BaseData.TEST_ACCOUNT_NUMBER_2)); //卡号哈希值
		request.setBizContent(bizContent);
		SettlementAccountStatusQueryResponseV1  response = client.execute(request);
		System.out.println(JSON.toJSONString(response));
		if (response.isSuccess() ) {
			// 业务成功处理
			System.out.println("II类卡号：" + SecretUtils.decryptMediumId(response.getMediumId(), response.getSecretKey()));
		} else {
			// 失败
		}

	}
}
